﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ninject;
using Ninject.Parameters;
using System.Globalization;
using TheOutLetLibrary.Dominio;
using TheOutLetLibrary.Infraestrutura;

namespace TheOutLetUI {

    public partial class FrmCargaOrder : Form {

        private MeuKernel kernel;
        private IFactoryDAO factoryDAO;

        private static FrmCargaOrder frmCargaOrder = null;

        public static FrmCargaOrder Instance() {
            if(frmCargaOrder == null) {
                frmCargaOrder = new FrmCargaOrder();
            }
            return frmCargaOrder;
        }

        private FrmCargaOrder() {
            InitializeComponent();
        }

        private void btnCarregarOrders_Click(object sender, EventArgs e) {
            Cursor.Current = Cursors.WaitCursor;
            DataGridOrders.Rows.Clear();
            labelStatus.Text = "Carregando lista de pedidos ...";
            Application.DoEvents();
            Carregadork_order carregadorOrder = new Carregadork_order(factoryDAO);
            List<int> orders = carregadorOrder.CarregueListaDeOrders(dataInicial.Value, dataFinal.Value);
            decimal qteCarregados = 0;
            foreach(int orderId in orders) {
                labelStatus.Text = String.Format("{0:#0%} Carregando pedido {1} ...", qteCarregados / orders.Count(), orderId.ToString());
                Application.DoEvents();
                k_orderLog orderLog = carregadorOrder.CarregueOrder(orderId);
                int row = DataGridOrders.Rows.Add();
                DataGridOrders.Rows[row].Cells[0].Value = orderLog.Order.NR_PEDIDO;
                DataGridOrders.Rows[row].Cells[1].Value = orderLog.Order.DateCreated;
                DataGridOrders.Rows[row].Cells[2].Value = orderLog.Order.Status;
                DataGridOrders.Rows[row].Cells[3].Value = orderLog.StatusDaCarga;
                qteCarregados++;
            }
            if(orders.Count > 0) {
                labelStatus.Text = String.Format("{0:#0%} Concluído", qteCarregados / orders.Count());
            } else {
                labelStatus.Text = "Não existem pedidos para o período informado";
            }

            Cursor.Current = Cursors.Default;
        }

        private void FrmCargaOrder_Load(object sender, EventArgs e) {
            kernel = new MeuKernel();
            factoryDAO = kernel.Get<IFactoryDAO>();
        }

    }
}
